//
//  JYSearchBar.m
//  JYFarm
//
//  Created by 花花 on 2017/10/10.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYSearchBar.h"

@interface JYSearchBar()<UITextFieldDelegate>
//bgView

//
@property (nonatomic, strong) UIView *searchBgView;

//
@property (nonatomic, strong) UIImageView *searchImgView;


//是否隐藏Label;
@property(nonatomic,assign)BOOL isHiddenLabel;

@end
@implementation JYSearchBar

- (instancetype)initWithOffSet:(NSInteger)offset{
    self = [super init];
    if (self) {
//        _isHiddenLabel = isHiddenLabel;
       
        [self setupSubView];
        [self initConstraintWithOffset:offset];
        [self initGesture];
    }
    return self;
}

//-(instancetype)init{
//    self = [self initWithOffSet:12];
//    return self;
//}

- (void)textFieldEditChanged:(UITextField *)textField
{
    if (self.searchBarInputCallBack) {
        self.searchBarInputCallBack(textField.text);
    }
}

- (void)initConstraintWithOffset:(NSInteger)offset{
    JYWeakSelf;
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.mas_top);
        make.bottom.equalTo(weakSelf.mas_bottom);
    }];
    CGFloat searchSpa = offset;
    CGFloat searchHeight = 32;
    CGFloat searchY = 25;
    
    [_searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.mas_left).offset(searchSpa);
        make.right.equalTo(weakSelf.mas_right).offset(-10);
        make.top.equalTo(weakSelf.mas_top).offset(searchY);
        make.height.equalTo(@(searchHeight));
    }];
    
    [_searchImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.searchBgView.mas_left);
        make.top.equalTo(weakSelf.searchBgView.mas_top);
        make.width.equalTo(@(searchHeight));
        make.height.equalTo(@(searchHeight));
    }];
    
    [_textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.searchImgView.mas_right);
        make.top.equalTo(weakSelf.searchBgView.mas_top);
        make.bottom.equalTo(weakSelf.searchBgView.mas_bottom);
        make.right.equalTo(weakSelf.searchBgView.mas_right);
    }];
}

- (void)setProgress:(CGFloat)progress{
    if (isnan(progress)) {
        progress = 0;
    }else if (progress >1) {
        progress = 1;
    }
    
    _progress = progress;
    _bgView.alpha = progress;
    _searchBgView.alpha = progress/10 + 0.9;
}

- (void)setSearchEnable:(BOOL)searchEnable{
    _searchEnable = searchEnable;
    _textFiled.enabled = searchEnable;
}

- (void)tapGestureAction{
    
    if (self.searchBarClickCallBack) {
        self.searchBarClickCallBack();
    }
}

- (void)setupSubView{

    self.backgroundColor = [UIColor clearColor];
    _bgView = [[UIImageView alloc]init];
    _bgView.image = [UIImage imageNamed:@"navigationbar_background"];
    _bgView.alpha = 0;
    [self addSubview:_bgView];
    
    
//    _nameLabel = [UILabel labelText:@"龙门客栈" fontSize:14 textColor:k3Color textAlignment:NSTextAlignmentCenter];
//    _nameLabel.layer.cornerRadius = 18;
//    _nameLabel.layer.masksToBounds = YES;
//    
//    _nameLabel.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:.5];
//    _nameLabel.hidden = _isHiddenLabel;
//    [self addSubview:_nameLabel];
    
    CGFloat searchHeight = 32;
    _searchBgView = [[UIView alloc]init];

    _searchBgView.backgroundColor = [UIColor colorWithHexString:@"EDEDED" alpha:.5];
    _searchBgView.layer.cornerRadius = searchHeight/2;
    _searchBgView.layer.masksToBounds = YES;
    [self addSubview:_searchBgView];
    
    _searchImgView = [[UIImageView alloc]init];
    _searchImgView.contentMode = UIViewContentModeCenter;
    _searchImgView.image = [UIImage imageNamed:@"home_search_sliding"];
    [_searchBgView addSubview:_searchImgView];
    
    _textFiled = [[UITextField alloc]init];
    _textFiled.returnKeyType = UIReturnKeySearch;
    [_textFiled addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _textFiled.delegate = self;
    _textFiled.enabled = false;
    _textFiled.font = kFont(14);
    _textFiled.textColor = [UIColor blackColor];
//    [_textFiled setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//    [_textFiled setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    _textFiled.text = @"请输入商品名称";
    [_searchBgView addSubview:_textFiled];


}

- (void)initGesture{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
    [self.searchBgView addGestureRecognizer:tap];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    
    return true;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    return true;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

@end
